package com.zl.learning.guava;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class TestCache {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Cache<String, String> cache = CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.SECONDS).build();
        cache.put("a", "a");
        cache.put("b", "b");
        TimeUnit.SECONDS.sleep(5);
        System.out.println(cache.get("a", () -> null));
        TimeUnit.SECONDS.sleep(7);
        System.out.println(cache.get("a", () -> null));
        System.out.println(cache.get("b", () -> "null"));
    }
}
